<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>计算属性的复杂操作</title>
  </head>
  <body>
    <div id="app">
      <h2>总价格为：{{totalPrice}}</h2>
    </div>
    <script src="../vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          books: [
            { id: 110, name: "编程之美", price: 70 },
            { id: 111, name: "算法", price: 88 },
            { id: 112, name: "六级真题", price: 20 },
            { id: 113, name: "笔记", price: 5 },
          ],
        },
        computed: {
          totalPrice: function () {
            let result = 0;
            for (let i = 0; i < this.books.length; i++) {
              result += this.books[i].price;
            }
            return result;
          },
        },
      });
    </script>
  </body>
</html>
